<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT admin " .
		"FROM users " .
		"WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
include_once( '../diplomacy/shoutbox_utils.php' );
$shoutbox = new Shoutbox();
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
?>
<html>
	<head>
		<title>Ages of Strife: Manage chat channels</title>
		<link type="text/css" rel="stylesheet" href="../global/agesofstrife.css" />
	</head>
	<body>
		<div id="root_container">
<?php
	include( "../global/ads.php" );
?>
			<div id="container">
<?php
	include_once( "../global/old_database_connection.php" );
?>
    			<div id="main-content">
<?php	
	if( isset( $_GET[ 'update_faction_channels' ] ) && $_GET[ 'update_faction_channels' ] == 'true' ) {
		update_faction_channels();
		print( 'All factions without a chat channel have one now.<br /><br />' );
	}
	if( isset( $_SESSION[ 'channel_submit_status' ] ) ) {
		print( "<div class=\"blok\">" . $_SESSION[ 'channel_submit_status' ] . "</div><br />\r\n" );
		unset( $_SESSION[ 'channel_submit_status' ] );
	}
		
	$query = "SELECT * " .
			"FROM chat_channels " .
			"WHERE group_id = -1 " .
			"ORDER BY name ASC";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	$i = 0;
?>
    				<div class="blok">
		    			<a href="admin.php">Back to the admin screen</a><br />
						<br />
						Note: Add &quot;?update_faction_channels=true&quot; to the URL to generate at least 1 chat channel for each faction.<br />
						<br />
						<strong>Public chat channels</strong><br />
						<ul>
<?php
	while ( $i < $num ) {
		print( "<li>" . mysql_result( $result, $i, "name" ) . " - <a href=\"chat_channels.php?id=" . mysql_result( $result, $i, "channel_id" ) . "\">Edit</a> - <a href=\"remove_chat_channel.php?channel_id=" . mysql_result( $result, $i, "channel_id" ) . "\">Remove</a>" . "</li>" );
		$i ++;
	}
	
	$channel_id = -1;
	if( isset( $_GET[ 'id' ] ) && is_numeric( $_GET[ 'id' ] ) ) {
		$channel_id = $_GET[ 'id' ];
	}
?>
						</ul>
						<br />
						<strong>Add new channel</strong><br />
						<?php $shoutbox->print_chat_channel_form( 'submit_chat_channel.php', -1, $channel_id ); ?>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
	<?php
}
else {
	$_SESSION[ 'admin' ] = 0;
	header( "Location: ../overview/overview.php" );
}
?>